﻿using Script.UI.Component;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HistogramGroup : MonoBehaviour
{

    public GridLayoutGroup Layout;
    private List<HistogramController> objList = new List<HistogramController>();

    private void Awake()
    {
        if (Layout == null)
        {
            gameObject.GetComponent<GridLayoutGroup>();
        }

        LoadHistogramController();

        
    }

    private void LoadHistogramController()
    {
        if (objList == null || objList.Count == 0)
        {
            var t = GetComponentsInChildren<HistogramController>(true);
            for (int i = 0; i < t.Length; i++)
            {
                objList.Add(t[i]);
            }
            //objList.Reverse();
        }
    }

    public void Refresh(int index, HistogramDataBase data)
    {
        if (objList.Count <= index)
        {
            return;
        }

        objList[index].Refresh(data);
    }

    public void Refresh(List<HistogramDataBase> list)
    {

        LoadHistogramController();

        int iNeedCount = 0;
        if (objList.Count != list.Count)
        {
            iNeedCount = Math.Min(objList.Count, list.Count);
        }
        else
        {
            iNeedCount = list.Count;
        }

        for (var i = 0; i < iNeedCount; i++)
        {
            HistogramController histogramController = objList[i];
            histogramController.gameObject.SetActive(true);
            histogramController.Refresh(list[i]);
        }

        for (int i = iNeedCount; i < objList.Count; i++)
        {
            objList[i].gameObject.SetActive(false);
        }
    }
}
